<?php
/*
 * Get latest activity.
 */

// check params
if(F3::exists("PARAMS['user_id']") && F3::exists("PARAMS['view_id']")):
	$id = F3::get("PARAMS['user_id']");
	$view = F3::get("PARAMS['view_id']");
else:
	header('HTTP/1.1 500 Internal Server Error');
	header("Content-Type: application/json");
	$err = array("error_message" => "Need a user id and a view id.");
	echo json_encode($err);
	return;
endif;

// check admin status
$param = array('1' => $id);
$admin = DB::sql("SELECT id from admin WHERE id=?", $param);
if(count($admin)>0):
	$admin = 1;
else:
	$admin = 0;
endif;

$response = array();

// get comments by people the user follows
$param = array('1'=>$id);
$comments = DB::sql("SELECT c.uid as id, c.rid, c.comment, c.timestamp, nickname, email, name, author from comments c, user, recipes r where c.rid=r.rid and c.uid=id and c.uid in (SELECT id2 FROM friends WHERE id1 =? ) order by timestamp desc",$param);

if(count($comments)>0):
	foreach($comments as $comment):
		if($view==0 || $view==$comment['id']):
			// is user allowed to see recipe?
			$param = array('1'=>$comment['rid']);
			$privacy = DB::sql("SELECT privacy from recipes where rid = ?", $param);
			$privacy = $privacy[0]['privacy'];
			
			$author = $comment['author'];
			$param = array('1'=>$author,'2'=>$id);
			$friend = DB::sql("SELECT id1 from friends where id1 = ? and id2 = ?", $param);
			if(!$admin):
				if(($privacy==1 && $id!=$author)):
					continue;
				elseif(($privacy==2 && count($friend)==0 && $id!=$author)):
					continue;
				endif;
			endif;
			
			// get tags
			$param = array('1'=>$comment['rid']);
			$tags = DB::sql("select id, name from tags, recipe_tags where rid=? and tid=id",$param);
			$comment['tags'] = array();
			foreach($tags as $tag):
				$comment['tags'][] = $tag;
			endforeach;
			$comment['type'] = "comment";
			$response[] = $comment;
		endif;
	endforeach;
endif;

// get ratings by people the user follows
$param = array('1'=>$id);
$ratings = DB::sql("SELECT ratings.rid, ratings.uid as id, ratings.rating, ratings.timestamp, nickname, email, name, author from ratings, recipes, user where ratings.rid=recipes.rid and uid=id and uid in (SELECT id2 FROM friends WHERE id1 =? ) order by timestamp desc",$param);

if(count($ratings)>0):
	foreach($ratings as $rating):
		if($view==0 || $view==$rating['id']):
			// is user allowed to see recipe?
			$param = array('1'=>$rating['rid']);
			$privacy = DB::sql("SELECT privacy from recipes where rid = ?", $param);
			$privacy = $privacy[0]['privacy'];
			
			$author = $rating['author'];
			$param = array('1'=>$author,'2'=>$id);
			$friend = DB::sql("SELECT id1 from friends where id1 = ? and id2 = ?", $param);
			if(!$admin):
				if(($privacy==1 && $id!=$author)):
					continue;
				elseif(($privacy==2 && count($friend)==0 && $id!=$author)):
					continue;
				endif;
			endif;
		
			// get tags
			$param = array('1'=>$rating['rid']);
			$tags = DB::sql("select name from tags, recipe_tags where rid=? and tid=id",$param);
			$avg_rating = DB::sql("select avg(rating) as avg_rating, count(rating) as rating_count from ratings where rid=? group by rid",$param);
			$rating['tags'] = array();
			foreach($tags as $tag):
				$rating['tags'][] = $tag['name'];
			endforeach;
			$rating['avg_rating'] = $avg_rating[0]['avg_rating'];
			$rating['rating_count'] = $avg_rating[0]['rating_count'];
			$rating['type'] = "rating";
			$response[] = $rating;
		endif;
	endforeach;
endif;

// get recipes by people the user follows
$param = array('1'=>$id);
$recipes = DB::sql("SELECT rid, name, author as id, date as timestamp, nickname, email from recipes, user where author=id and author in (SELECT id2 FROM friends WHERE id1 =? ) order by Date desc",$param);

if(count($recipes)>0):
	foreach($recipes as $recipe):
		if($view==0 || $view==$recipe['id']):
			// is user allowed to see recipe?
			$param = array('1'=>$recipe['rid']);
			$privacy = DB::sql("SELECT privacy from recipes where rid = ?", $param);
			$privacy = $privacy[0]['privacy'];
			
			$author = $recipe['id'];
			$param = array('1'=>$author,'2'=>$id);
			$friend = DB::sql("SELECT id1 from friends where id1 = ? and id2 = ?", $param);
			if(!$admin):
				if(($privacy==1 && $id!=$author)):
					continue;
				elseif(($privacy==2 && count($friend)==0 && $id!=$author)):
					continue;
				endif;
			endif;
			
			
			// get tags
			$param = array('1'=>$recipe['rid']);
			$tags = DB::sql("select id, name from tags, recipe_tags where rid=? and tid=id",$param);
			$recipe['tags'] = array();
			foreach($tags as $tag):
				$recipe['tags'][] = $tag;
			endforeach;
			$recipe['type'] = "recipe";
			$response[] = $recipe;
		endif;
	endforeach;
endif;

// get favorites by people the user follows
$param = array('1'=>$id);
$favorites = DB::sql("SELECT uid as id, recipes.rid, timestamp, nickname, email, name, author from favorites, user, recipes where favorites.rid=recipes.rid and uid=id and uid in (SELECT id2 FROM friends WHERE id1 =? ) order by timestamp desc",$param);

if(count($favorites)>0):
	foreach($favorites as $favorite):
		if($view==0 || $view==$favorite['id']):
			// is user allowed to see recipe?
			$param = array('1'=>$favorite['rid']);
			$privacy = DB::sql("SELECT privacy from recipes where rid = ?", $param);
			$privacy = $privacy[0]['privacy'];
			
			$author = $favorite['id'];
			$param = array('1'=>$author,'2'=>$id);
			$friend = DB::sql("SELECT id1 from friends where id1 = ? and id2 = ?", $param);
			if(!$admin):
				if(($privacy==1 && $id!=$author)):
					continue;
				elseif(($privacy==2 && count($friend)==0 && $id!=$author)):
					continue;
				endif;
			endif;
			
			
			// get tags
			$param = array('1'=>$favorite['rid']);
			$tags = DB::sql("select id, name from tags, recipe_tags where rid=? and tid=id",$param);
			$favorite['tags'] = array();
			foreach($tags as $tag):
				$favorite['tags'][] = $tag;
			endforeach;
			$favorite['type'] = "favorite";
			$response[] = $favorite;
		endif;
	endforeach;
endif;

aasort($response,"timestamp");
$response = array_reverse($response,true);

echo json_encode($response);
return;

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

?>
